﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;
using MobileWebsiteGenerator.Utility;
using MobileWebsiteGenerator.Models;
using System.IO;

namespace MobileWebsiteGenerator.Controllers
{
    public class ThemeDetailController : BaseController
    {
        public Mobile_appContext context = new Mobile_appContext();

        public ActionResult Index()
        {
            MvcApplication._MainCustomer.Website = context.Websites.Where(w => w.web_id == 1).FirstOrDefault();
            var themes = context.WebthemeRels.Where(w => w.Web_id == 1).Select(t => t.Theme_id);
            return View(context.ThemeDetails.Where(t => themes.Contains(t.theme_id)).ToList());
        }




        public string Upload(HttpPostedFileBase fileData)
        {

            var fileName = this.Server.MapPath("/Uploads/" + System.IO.Path.GetFileName(fileData.FileName));
            fileData.SaveAs(fileName);

            return Path.GetFileName(fileName);
        }


        public ActionResult ChangeSetting(int id)
        {
            return View(MvcApplication._MainCustomer);
        }


        [HttpPost]
        public ActionResult ChangeSetting(MainViewModel CurrentTheme)
        {




            if (ModelState.IsValid)
            {
                MvcApplication._MainCustomer.MyTheme.Headercolor = CurrentTheme.MyTheme.Headercolor;
                MvcApplication._MainCustomer.MyTheme.HeadertextColor = CurrentTheme.MyTheme.HeadertextColor;
                MvcApplication._MainCustomer.MyTheme.FootertextColor = CurrentTheme.MyTheme.FootertextColor;
                MvcApplication._MainCustomer.MyTheme.HeaderImage = CurrentTheme.MyTheme.HeaderImage;
                MvcApplication._MainCustomer.MyTheme.HeaderText = CurrentTheme.MyTheme.HeaderText;
                MvcApplication._MainCustomer.MyTheme.HHeaderText = CurrentTheme.MyTheme.HHeaderText;
                MvcApplication._MainCustomer.MyTheme.Image = CurrentTheme.MyTheme.Image;
                MvcApplication._MainCustomer.MyTheme.Footertext = CurrentTheme.MyTheme.Footertext;
                MvcApplication._MainCustomer.MyTheme.HFootertext = CurrentTheme.MyTheme.HFootertext;
                MvcApplication._MainCustomer.MyTheme.backgroundColor = CurrentTheme.MyTheme.backgroundColor;
                MvcApplication._MainCustomer.MyTheme.backgroundImage = CurrentTheme.MyTheme.backgroundImage;

                MvcApplication._MainCustomer.MyTheme.BannerImage = CurrentTheme.MyTheme.BannerImage;
                MvcApplication._MainCustomer.MyTheme.Bannertext = CurrentTheme.MyTheme.Bannertext;

                MvcApplication._MainCustomer.MyTheme.Border = CurrentTheme.MyTheme.Border;
                MvcApplication._MainCustomer.MyTheme.Size = CurrentTheme.MyTheme.Size;
                MvcApplication._MainCustomer.MyTheme.ButtonPicture = CurrentTheme.MyTheme.ButtonPicture;



                MvcApplication._MainCustomer.MyTheme.facebookurl = CurrentTheme.MyTheme.facebookurl;
                MvcApplication._MainCustomer.MyTheme.gplusurl = CurrentTheme.MyTheme.gplusurl;
                MvcApplication._MainCustomer.MyTheme.twitterurl = CurrentTheme.MyTheme.twitterurl;
                MvcApplication._MainCustomer.MyTheme.Isheader = CurrentTheme.MyTheme.Isheader;
                MvcApplication._MainCustomer.MyTheme.IsFooter = CurrentTheme.MyTheme.IsFooter;




                return View(CurrentTheme);

            }
            return View();
        }

        public ActionResult test()
        {
            return View();
        }






        public ActionResult Edit(int id)
        {
            MvcApplication._MainCustomer.MyTheme = context.ThemeDetails.Where(t => t.theme_id == id).FirstOrDefault();
            //var webDetail = context.Websites.Where(w => w.web_id == 2).FirstOrDefault();
            //MvcApplication._MainCustomer.Website = webDetail;            
            MvcApplication._MainCustomer.ContentPages = context.Pages.Where(p => context.ThemePageRELs.Where(c => c.Theme_id == MvcApplication._MainCustomer.MyTheme.theme_id).Select(x => x.Page_id).Contains(p.page_id)).ToList();
            MvcApplication._MainCustomer.PictureGallery = context.pictureGalleries.Where(p => context.ThemePictureRELs.Where(c => c.Theme_id == MvcApplication._MainCustomer.MyTheme.theme_id).Select(x => x.Picture_id).Contains(p.picture_id)).ToList();
            return Redirect(Url.Action("Create", "Website"));

        }

        public ActionResult SetCulture(string culture)
        {
            // Validate input
            culture = CultureHelper.GetValidCulture(culture);

            // Save culture in a cookie
            HttpCookie cookie = Request.Cookies["_culture"];
            if (cookie != null)
                cookie.Value = culture;   // update cookie value
            else
            {

                cookie = new HttpCookie("_culture");
                cookie.HttpOnly = false; // Not accessible by JS.
                cookie.Value = culture;
                cookie.Expires = DateTime.Now.AddYears(1);
            }
            Response.Cookies.Add(cookie);

            return RedirectToAction("Index", "ThemeDetail");
        }
    }
}
